#!/bin/bash

### BEGIN INIT INFO
# Provides:          setup-board
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Setup Inventec d6356j board.
### END INIT INFO

PLATFORM_DIR=/usr/share/sonic/device/x86_64-inventec_d6332-r0/plugins
PLUGIN_DIR=/usr/lib/python2.7/dist-packages/inventec_plugin

PLATFORM_DAEMON=$PLATFORM_DIR/platfmgr.py
PLATFORM_DAEMON_NAME=platfmgr
INVSYNCD_DAEMON=$PLUGIN_DIR/invSyncd.py
INVSYNCD_DAEMON_NAME=invSyncd

# The process ID of the script when it runs is stored here:
PLATFORM_PIDFILE=/var/run/$PLATFORM_DAEMON_NAME.pid

do_monitor_start() {
    /sbin/start-stop-daemon --quiet --oknodo --pidfile $PLATFORM_PIDFILE --make-pidfile --startas $PLATFORM_DAEMON --start --background -- $DAEMON_OPTS
    /sbin/start-stop-daemon --quiet --oknodo --pidfile $INVSYNCD_PIDFILE --make-pidfile --startas $INVSYNCD_DAEMON --start --background -- $DAEMON_OPTS
}

do_monitor_stop() {
    /sbin/start-stop-daemon --quiet --oknodo --stop --pidfile $PLATFORM_PIDFILE --retry 10
    /sbin/start-stop-daemon --quiet --oknodo --stop --pidfile $INVSYNCD_PIDFILE --retry 10
}

# Check Fast-Reboot cause
FAST_REBOOT='no'
case "$(cat /proc/cmdline)" in
  *fast-reboot*|*warm*)
    FAST_REBOOT='yes'
    ;;
  *)
    FAST_REBOOT='no'
    ;;
esac

case "$1" in
start)
        echo -n "Setting up board... "
        if [ "$FAST_REBOOT" = "yes" ] ; then
            /usr/local/bin/inventec_d6332_util.py -f fast-reboot-install
        else
            /usr/local/bin/inventec_d6332_util.py -f install
        fi
        do_monitor_${1}
        echo "done."
	;;

stop)
        /usr/local/bin/inventec_d6332_util.py -f clean
        do_monitor_${1}
	echo "done."
	;;

force-reload|restart)
	echo "Not supported"
	;;

*)
	echo "Usage: /etc/init.d/platform-modules-d6332.init {start|stop}"
	exit 1
	;;
esac

exit 0
